/**
 * @file qq_add_friend.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_QQ != 0
/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void qq_add_friend_input_btn_event_cb(lv_obj_t * btn, lv_event_t event);
static void qq_friend_info_add_friend_btn_event_cb(lv_obj_t * btn, lv_event_t event);
static void qq_lookup_friend(lv_obj_t * ta);
static void qq_destroy_current_ui_timer_cb(lv_anim_t * anim);
/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
void qq_create_add_friend(void)
{
    uint8_t side_pad = 8 + QQ_PAD_HOR;
    uint8_t inner_pad = 3;
    lv_qq_obj_ext_t * ext = qq_get_ext();
    lv_obj_set_hidden(ext->leds_content, true);
    lv_obj_t * par = ext->friend;
    lv_obj_t * bg_cont = qq_create_bg_cont(par, LV_OPA_COVER, 0);

    lv_obj_t * ta = lv_textarea_create(bg_cont, NULL);
    lv_obj_set_width(ta, LV_HOR_RES - side_pad * 2 - QQ_PAD_HOR * 2);
    lv_textarea_set_max_length(ta, 11);
    lv_textarea_set_one_line(ta, true);
    lv_obj_set_style_local_bg_color(ta, LV_TEXTAREA_PART_BG, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_textarea_set_text(ta, (char *)lv_lang_get_text(WATCH_TEXT_ID_QQ_INPUT_QQ_NUM));
    lv_textarea_set_text_align(ta, LV_LABEL_ALIGN_RIGHT);
    lv_textarea_set_cursor_hidden(ta, true);
    lv_obj_align(ta, bg_cont, LV_ALIGN_IN_TOP_MID, 0, lv_obj_get_height(ta) / 4 + QQ_PAD_VER);

    lv_obj_t * back_img = qq_create_back_img(ta, QQ_INPUT_NUM);
    lv_obj_align(back_img, ta, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    char * key[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "x", "0", "l"};
    lv_obj_t * key_btn;
    lv_obj_t * key_label;
    lv_obj_t * key_cont = lv_cont_create(bg_cont, NULL);
    lv_obj_set_size(key_cont, LV_HOR_RES, LV_VER_RES - lv_obj_get_height(ta) * 3 / 2);
    lv_obj_align(key_cont, bg_cont, LV_ALIGN_IN_BOTTOM_MID, 0, QQ_PAD_VER);
    lv_cont_set_layout(key_cont, LV_LAYOUT_GRID);
    lv_obj_set_style_local_bg_opa(key_cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_0);
    lv_obj_set_style_local_pad_left(key_cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, side_pad);
    lv_obj_set_style_local_pad_inner(key_cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, inner_pad);
    for(uint8_t i = 0; i < 12; i++) {
        key_btn = lv_btn_create(key_cont, NULL);
        lv_obj_set_size(key_btn, (lv_obj_get_width(key_cont) - (side_pad + inner_pad) * 2) / 3,
                        (lv_obj_get_height(key_cont) - inner_pad * 2 - side_pad) / 4);

        key_label = lv_label_create(key_btn, NULL);
        lv_obj_set_style_local_radius(key_btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 5);
        lv_obj_set_style_local_text_color(key_label, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
        if(i == 9) {
            lv_obj_set_style_local_bg_color(key_btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, QQ_COLOR_RED_NORMAL);
            lv_obj_set_style_local_bg_color(key_btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, QQ_COLOR_RED_PRESSED);
            lv_label_set_text_id(key_label, WATCH_TEXT_ID_DELETE);
        } else if(i == 11) {
            lv_obj_set_style_local_bg_color(key_btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, QQ_COLOR_GREEN_NORMAL);
            lv_obj_set_style_local_bg_color(key_btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, QQ_COLOR_GREEN_PRESSED);
            lv_label_set_text_id(key_label, WATCH_TEXT_ID_QQ_LOOKUP);
        } else {
            lv_obj_set_style_local_bg_color(key_btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, QQ_COLOR_BLUE_NORMAL);
            lv_obj_set_style_local_bg_color(key_btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, QQ_COLOR_BLUE_PRESSED);
            lv_label_set_text(key_label, key[i]);
        }
        lv_watch_set_free_num(key_btn, key[i][0]);
        lv_obj_set_event_cb(key_btn, qq_add_friend_input_btn_event_cb);
    }
}

void qq_add_friend_req_result(uint8_t result)
{
    lv_qq_obj_ext_t * ext = qq_get_ext();
    lv_obj_t * bg_cont = lv_obj_get_child(ext->friend, NULL);
    lv_obj_t * search_cont = lv_obj_get_child(bg_cont, NULL);
    lv_obj_t * label = lv_obj_get_child(lv_obj_get_child(search_cont, NULL), NULL);
    if(result == WATCH_QQ_OK)
        lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_ADD_FRIEND_REQ_SENT);
    else
        lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_ADD_FRIEND_REQ_FAILED);
    lv_obj_realign(label);
    lv_anim_t a = {0};
    lv_anim_init(&a);
    lv_anim_set_var(&a, lv_obj_get_child(search_cont, NULL));
    lv_anim_set_ready_cb(&a, (lv_anim_ready_cb_t)qq_destroy_current_ui_timer_cb);
    lv_anim_set_repeat_count(&a, 1);
    lv_anim_set_time(&a, 1000);
    lv_anim_start(&a);
}

void qq_show_find_friend_failed(void)
{
    lv_qq_obj_ext_t * ext = qq_get_ext();
    lv_obj_t * bg_cont = lv_obj_get_child(ext->friend, NULL);
    lv_obj_t * search_cont = lv_obj_get_child(bg_cont, NULL);
    lv_obj_t * ta = lv_obj_get_child_back(bg_cont, NULL);
    lv_obj_t * label = lv_obj_get_child(search_cont, NULL);
    lv_obj_set_height(search_cont, lv_obj_get_height(ta) * 3 / 2);
    lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_NO_INFO);
    lv_obj_realign(label);
    lv_anim_t a = {0};
    lv_anim_init(&a);
    lv_anim_set_var(&a, search_cont);
    lv_anim_set_ready_cb(&a, (lv_anim_ready_cb_t)qq_destroy_current_ui_timer_cb);
    lv_anim_set_repeat_count(&a, 1);
    lv_anim_set_time(&a, 1000);
    lv_anim_start(&a);
}

void qq_get_friend_info(char * open_id)
{
    lv_qq_obj_ext_t * ext = qq_get_ext();
    lv_obj_t * bg_cont = lv_obj_get_child(ext->friend, NULL);
    lv_obj_t * search_cont = lv_obj_get_child(bg_cont, NULL);
    lv_obj_t * label = lv_obj_get_child(search_cont, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_PULLING_DATA);
    lv_obj_realign(label);

#if QQ_UI_TEST
    Hal_Timer_Start(1000, qq_show_friend_info_timer_cb, NULL, false);
#else
    watch_qq_get_friend_info_req_t * get_friend = (watch_qq_get_friend_info_req_t *)lv_mem_alloc(sizeof(watch_qq_get_friend_info_req_t));
    memset(get_friend, 0, sizeof(watch_qq_get_friend_info_req_t));
    memcpy(get_friend->openid, open_id, 64);
    qq_set_get_friend_info_type(QQ_FIND_FRIEND_INFO);
    watch_qq_get_friend_info_req(get_friend);
#endif
}

void qq_show_friend_info(watch_qq_user_info_t * info)
{
    lv_qq_obj_ext_t * ext = qq_get_ext();
    lv_obj_t * bg_cont = lv_obj_get_child(ext->friend, NULL);
    lv_obj_t * search_cont = lv_obj_get_child(bg_cont, NULL);
    lv_obj_set_style_local_bg_opa(search_cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_COVER);
    lv_obj_set_style_local_bg_color(search_cont, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_obj_clean(search_cont);

    qq_create_back_img(search_cont, QQ_SEARCH_FRIEND_INFO);
    lv_obj_t * head_img = lv_img_create(search_cont, NULL);
    lv_img_set_src(head_img, ICON_QQ_DEF_HEAD_MID);
    lv_obj_align(head_img, NULL, LV_ALIGN_IN_TOP_MID, 0, lv_obj_get_height(head_img) * 2 / 3 + QQ_PAD_VER);

    lv_obj_t * name_label = lv_label_create(search_cont, NULL);
    lv_label_set_text(name_label, info->nickname);
    lv_obj_set_style_local_text_color(name_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_label_set_long_mode(name_label, LV_LABEL_LONG_DOT);
    lv_obj_set_width(name_label, LV_HOR_RES * 2 / 3);
    lv_label_set_align(name_label, LV_LABEL_ALIGN_CENTER);
    lv_obj_align(name_label, head_img, LV_ALIGN_OUT_BOTTOM_MID, 0, lv_obj_get_height(head_img) / 4);

    lv_obj_t * num_label = lv_label_create(search_cont, NULL);
    lv_label_set_text(num_label, info->qqid);
    lv_obj_set_style_local_text_color(num_label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_obj_align(num_label, name_label, LV_ALIGN_OUT_BOTTOM_MID, 0, lv_obj_get_height(head_img) / 4);

    lv_obj_t * btn = lv_btn_create(search_cont, NULL);
    lv_obj_set_size(btn, LV_HOR_RES * 5 / 6 - QQ_PAD_HOR * 2, LV_VER_RES / 5 - QQ_PAD_VER);
    lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_COLOR_WHITE);
    lv_obj_set_style_local_radius(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 50);
    qq_btn_allocate_ext(btn, info);
    lv_obj_set_event_cb(btn, qq_friend_info_add_friend_btn_event_cb);
    lv_obj_t * label = lv_label_create(btn, NULL);
    lv_label_set_text_id(label, WATCH_TEXT_ID_QQ_ADD_FRIEND);
    lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_obj_align(btn, num_label, LV_ALIGN_OUT_BOTTOM_MID, 0, lv_obj_get_height(head_img) / 4);
}
/**********************
 *   STATIC FUNCTIONS
 **********************/
static void qq_destroy_current_ui_timer_cb(lv_anim_t * anim)
{
    lv_obj_del((lv_obj_t *)anim->var);
}

static void qq_add_friend_input_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;
    lv_obj_t * bg_cont = lv_obj_get_parent(lv_obj_get_parent(btn));
    lv_obj_t * ta = lv_obj_get_child_back(bg_cont, NULL);
    char key = lv_obj_get_user_data(btn).user_num;
    bool no_num_input = false;
    if(strncmp(lv_textarea_get_text(ta), (char *)lv_lang_get_text(WATCH_TEXT_ID_QQ_INPUT_QQ_NUM),
               strlen((char *)lv_lang_get_text(WATCH_TEXT_ID_QQ_INPUT_QQ_NUM))) == 0) {
        no_num_input = true;
    }
    if(('0' <= key) && ('9' >= key)) {
        if(no_num_input) {
            lv_textarea_set_text(ta, "");
        }
        lv_textarea_add_char(ta, key);
    } else if('x' == key) {
        if(no_num_input) return;
        lv_textarea_del_char(ta);
        if(strlen(lv_textarea_get_text(ta)) == 0) {
            lv_textarea_set_text(ta, (char *)lv_lang_get_text(WATCH_TEXT_ID_QQ_INPUT_QQ_NUM));
        }
    } else if('l' == key) {
        if(no_num_input) return;
        qq_lookup_friend(ta);
    }
}

static void qq_friend_info_add_friend_btn_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED == event) {
        lv_obj_t * search_cont = lv_obj_get_parent(btn);
        qq_create_bg_cont(search_cont, LV_OPA_90, WATCH_TEXT_ID_QQ_OPERATING);
#if QQ_UI_TEST
        Hal_Timer_Start(1000, qq_add_friend_timer_cb, NULL, false);
#else
        qq_friend_btn_ext_t * btn_ext = lv_obj_get_ext_attr(btn);
        watch_qq_add_friend_req_t * req = (watch_qq_add_friend_req_t *)lv_mem_alloc(sizeof(watch_qq_add_friend_req_t));
        memset(req, 0, sizeof(watch_qq_add_friend_req_t));
        watch_qq_login_info_t * reg_info = watch_qq_get_last_login_info();
        memcpy(req->from_openid, reg_info->openid, 64);
        memcpy(req->to_openid, btn_ext->info.openid, 64);
        req->validation_msg = NULL;
        req->sourceid = WATCH_QQ_SOURCE_FROM_QQ_ID;
        req->remark = NULL;
        req->myfriend_groupid = 0;
        watch_qq_add_friend_req(req);
        lv_mem_free(reg_info);
#endif
    } else if(LV_EVENT_DELETE == event) {
        qq_friend_btn_ext_t * btn_ext = lv_obj_get_ext_attr(btn);
        if(btn_ext->info.headurl) lv_mem_free(btn_ext->info.headurl);
        if(btn_ext->info.nickname) lv_mem_free(btn_ext->info.nickname);
        if(btn_ext->info.remark) lv_mem_free(btn_ext->info.remark);
    }
}

static void qq_lookup_friend(lv_obj_t * ta)
{
    char * qq_id = (char *)lv_textarea_get_text(ta);
    printf("%s: qqid = %s\n", __FUNCTION__, qq_id);
    lv_obj_t * bg_cont = lv_obj_get_parent(ta);
    qq_create_bg_cont(bg_cont, LV_OPA_90, WATCH_TEXT_ID_QQ_SEARCHING);

#if QQ_UI_TEST
    static uint8_t res = 0;
    if(strncmp(qq_id, "416656990", 9) == 0)
        res = 1;
    else
        res = 0;
    Hal_Timer_Start(1000, qq_find_friend_result_timer_cb, (void *)&res, false);
#else
    watch_qq_find_friend_req_t * find_friend = (watch_qq_find_friend_req_t *)lv_mem_alloc(sizeof(watch_qq_find_friend_req_t));
    memset(find_friend, 0, sizeof(watch_qq_find_friend_req_t));
    memcpy(find_friend->qq_id, qq_id, strlen(qq_id));
    watch_qq_find_friend_req(find_friend);
#endif
}

#endif /*USE_LV_WATCH_QQ*/
